package com.ncs.cns.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.OnMapScreenShotListener;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.LocationSource.OnLocationChangedListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.MyLocationStyle;
import com.ncs.cns.R;

public class MyPositionActivity extends Activity {
	
	private AMap aMap;
	private MapView mapView;
	private OnLocationChangedListener mListener;
	private LocationManagerProxy mAMapLocationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_position);
		
		mapView = (MapView) findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);// 此方法必须重写
		init();
		
				
		Button sure = (Button)findViewById(R.id.add_myposition);
		sure.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				aMap.getMapScreenShot(screenshotListener);
				
			}			
		});
	}

	/**
	 * 初始化AMap对象
	 */
	private void init() {
		if (aMap == null) {
			aMap = mapView.getMap();
			UiSettings setting = aMap.getUiSettings();
			setting.setZoomControlsEnabled(false);
			setUpMap();
		}
	}

	/**
	 * 设置一些amap的属性
	 */
	private void setUpMap() {
		// 自定义系统定位小蓝点
		MyLocationStyle myLocationStyle = new MyLocationStyle();
		myLocationStyle.myLocationIcon(BitmapDescriptorFactory
				.defaultMarker(BitmapDescriptorFactory.HUE_RED));// 设置小蓝点的图标
		
		//myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色
		// myLocationStyle.radiusFillColor(color)//设置圆形的填充颜色
		// myLocationStyle.anchor(int,int)//设置小蓝点的锚点
		myLocationStyle.strokeWidth(0.0f);// 设置圆形的边框粗细
		aMap.setMyLocationStyle(myLocationStyle);
		aMap.setMyLocationRotateAngle(180);
		
		//aMap.setTrafficEnabled(false);
		aMap.setLocationSource(locationSource);// 设置定位监听
		aMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
		aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false

		
	}
	
	private OnMapScreenShotListener screenshotListener = new OnMapScreenShotListener(){
		public void onMapScreenShot(Bitmap bitmap) {
			if(bitmap != null){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US); 
				String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp";
				File dir = new File(filePath);
				if(!dir.exists()){
					dir.mkdirs();
				}
				String fileName = filePath + "/" + sdf.format(new Date()) + ".png";
				try{
					FileOutputStream fos = new FileOutputStream(fileName);
					bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
					fos.flush();
					fos.close();
					
					Intent intent = getIntent();
					intent.putExtra("filePath", fileName);
					MyPositionActivity.this.setResult(4, intent);
					MyPositionActivity.this.finish();
					
				}catch(Exception e){
					Toast.makeText(MyPositionActivity.this, "截屏失败",Toast.LENGTH_LONG).show();
				}
			}
		}
	};
	
	private LocationSource locationSource = new LocationSource(){
		/**
		 * 激活定位
		 */
		@Override
		public void activate(OnLocationChangedListener listener) {
			mListener = listener;
			if (mAMapLocationManager == null) {
				mAMapLocationManager = LocationManagerProxy.getInstance(MyPositionActivity.this);
				/*
				 * mAMapLocManager.setGpsEnable(false);
				 * 1.0.2版本新增方法，设置true表示混合定位中包含gps定位，false表示纯网络定位，默认是true Location
				 * API定位采用GPS和网络混合定位方式
				 * ，第一个参数是定位provider，第二个参数时间最短是5000毫秒，第三个参数距离间隔单位是米，第四个参数是定位监听者
				 */
				mAMapLocationManager.requestLocationUpdates(
						LocationProviderProxy.AMapNetwork, 5000, 10, locationListener);
			}
		}

		/**
		 * 停止定位
		 */
		@Override
		public void deactivate() {
			mListener = null;
			if (mAMapLocationManager != null) {
				mAMapLocationManager.removeUpdates(locationListener);
				mAMapLocationManager.destory();
			}
			mAMapLocationManager = null;
		}
		
	};
	
	private AMapLocationListener locationListener = new AMapLocationListener(){
		/**
		 * 此方法已经废弃
		 */
		@Override
		public void onLocationChanged(Location location) {
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}

		/**
		 * 定位成功后回调函数
		 */
		@Override
		public void onLocationChanged(AMapLocation aLocation) {
			if (mListener != null && aLocation != null) {
				mListener.onLocationChanged(aLocation);// 显示系统小蓝点
				float bearing = aMap.getCameraPosition().bearing;
				aMap.setMyLocationRotateAngle(bearing);// 设置小蓝点旋转角度
				aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
				locationSource.deactivate();
			}
		}
		
	};
	
	/**
	 * 方法必须重写
	 */
	@Override
	protected void onResume() {
		super.onResume();
		mapView.onResume();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
		locationSource.deactivate();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}
}
